<!--
 * @Author: your name
 * @Date: 2021-08-20 16:43:53
 * @LastEditTime: 2021-08-29 19:15:53
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue电商管理平台\vue-e-commerce\src\components\Home.vue
-->


<template>
  <el-container class="home_container">
    <el-header
      ><div>
        <img src="../assets/logo.jpg" alt="" />
        <span>后台管理系统</span>
      </div>
      <el-button @click="logout" type="info">退出</el-button></el-header
    >
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <el-menu
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="activePath"
          background-color="#3e4c69"
          text-color="#fff"
          unique-opened
          router
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="icons[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
        <div class="fold_button" @click="handleFold">></div>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      icons: {
        [125]: "iconfont icon-user",
        [103]: "iconfont icon-tijikongjian",
        [101]: "iconfont icon-shangpin",
        [102]: "iconfont icon-danju",
        [145]: "iconfont icon-baobiao",
      },
      //左侧菜单是否折叠
      isCollapse: false,
      //激活的选项
      activePath: "",
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    async getMenuList() {
      const { data: res } = await this.$axios.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
    },
    handleFold() {
      this.isCollapse = !this.isCollapse;
    },
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
    },
    logout() {
      //删除 sessionStorage 中的 token
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
  },
};
</script>

<style scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #2e3950;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-header div {
  display: flex;
  align-items: center;
}
.el-header div img {
  width: 40px;
  border-radius: 50%;
}
.el-header div span {
  color: #fff;
  font-size: 20px;
  margin-left: 15px;
}

.el-aside {
  background-color: #232d42;
  position: relative;
}
.el-aside .el-menu {
  border-right: none;
}
.el-aside .fold_button {
  /* $fold-height: 40px; */
  background-color: #1e2638;
  color: #fff;
  height: 40px;
  width: 100%;
  font-size: 20px;
  line-height: 40px;
  text-align: center;
  box-sizing: border-box;
  position: absolute;
  bottom: 0;
  cursor: pointer;
}

.el-main {
  background-color: #eee;
}

.iconfont {
  margin-right: 10px;
}
</style>>

